<template>

	<div class=" news-list">
		<div v-for="(card, index) in cardList" :key="index" class="card" @click="navigateToDetail(card.id)">

			<div class="header">
				<text class="title">{{ card.title }}</text>
			</div>
			<div class="content-row">
				<image class="image" :src="card.cover"></image>
				<div class="content-container">
					<text class="description">{{ card.text }}</text>
					<div class="footer">
						<u-icon name="clock-fill" color="#666" size="14"  ></u-icon>
						<text class="date">日期{{ card.date }}</text>
					</div>
				</div>
			</div>

		</div>

		<van-pagination v-model:current-page="currentPage" :total-items="totalItems" :items-per-page="itemsPerPage"
			@change="fetchFoodList" />
	</div>
</template>

<script>
	import {
		listFood
	} from "@/api/system/food";
	import NewsCard from "@/components/wrap/NewsCard.vue";

	export default {
		data() {
			return {
				cardList: [],
				currentPage: 1,
				totalItems: 0,
				itemsPerPage: 4,
				loading: true,
			};
		},
		methods: {
			async fetchFoodList(page = 1) {
				this.loading = true;
				try {
					const response = await listFood({
						pageNum: page,
						pageSize: this.itemsPerPage,
					});
					if (response && response.code === 200) {
						const {
							rows,
							total
						} = response;
						this.totalItems = total;
						this.cardList = rows.map((food) => ({
							title: food.title,
							cover: food.cover || "default-image-url",
							text: food.info || "No information available",
							id: food.id,
							date: food.createTime,
						}));
					} else {
						console.error("Failed to fetch Food list:", response.msg || "Unknown error");
					}
				} catch (error) {
					console.error("Failed to fetch Food list:", error);
				} finally {
					this.loading = false;
				}
			},
			navigateToDetail(id) {
				this.$tab.navigateTo(`/pages/food/food?id=${id}`);
			},
		},
		created() {
			this.fetchFoodList();
		},
	};
</script>

<style scoped>
	.card {
		background-color: white;
		    position: relative;
		    /* width: calc(100% - 40px); */
		    max-width: 600px;
		    min-height: 120px;
		    padding: 10px;
		    margin: 10px;
		    border-radius: 15px;
		    overflow: hidden;
		    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
		    cursor: pointer;
		    border: 1px solid #ccc;
		    transition: box-shadow 0.3s ease, -webkit-transform 0.3s ease;
		    transition: transform 0.3s ease, box-shadow 0.3s ease;
		    transition: transform 0.3s ease, box-shadow 0.3s ease, -webkit-transform 0.3s ease;
	}

	.card:hover {
		transform: scale(1.05);
		box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
	}

	.content-row {
		display: flex;
		flex-direction: row;
	}

	.image {
		flex: 1;
		width: 100%;
		min-height: 80px;
		height: auto;
		border-radius: 4px;
		margin-right: 16px;
	}

	.content-container {
		flex: 2;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.description {
		font-size: 14px;
		color: #555;
		margin-bottom: 12px;
	}

	.footer {
		display: flex;
		align-items: center;
	}

	.iconfont {
		font-size: 16px;
		margin-right: 8px;
	}

	.date {
		font-size: 12px;
		color: #888;
	}

	.van-pagination {
		padding-bottom: 20px;
	}

	.card-list-container {
		padding: 0px;
		background-color: #f0fff0;
	}

	.news-list {
		display: flex;
		flex-direction: column;
	}

	.card-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		align-items: flex-start;
		min-height: 70vh;
	}

	.title {
		font-size: 18px;
		padding: 0;
		display: block;
		font-weight: bold;
		width: 100%;
		margin: 0;
		height: 36px;
		line-height: 24px;
	}
</style>